<?php
class indexController extends RPHP_Controller
{

    public function gateway()
    {
        if ($_GET['recode'] != "") {
            $recode = addslashes($_GET['recode']);
            if ($recode != '') {
                $_SESSION['qrcode_recode'] = $recode;
            }
        }

        $this->redirect("/");
    }

    public function index()
    {

        $m    = $this->model;
        $user = $m->checkAuth();
        if ($user) {

            $next = true;

            if ($_GET['recode'] != "") {
                $recode = addslashes($_GET['recode']);
                if ($recode != '') {
                    $_SESSION['qrcode_recode'] = $recode;
                }
            } else {
                $pro = $m->getRecode();
                if ($pro['recode'] != "") {
                    $this->redirect("/?recode=" . $pro['recode']);
                    $next = false;
                }
            }

            if ($next) {
               // $this->view->JSAPI = $m->getJSApiData();
                //dd($this->view->JSAPI);
                $data              = $m->indexList();
                //dd($data);
                $this->view->data  = $data;
                $this->view->user  = $user;
                $this->view->config  = $m->getSetting();
                $this->display("index");
            }

        }

    }

    public function ajaxSpecial()
    {


        $m    = $this->model;
        $user = $m->checkAuth();
        if ($user) {

            $data = $m->ajaxGetSpecial();

            echo json_encode($data);die();
        }
       // $this->table("today_special")->findBy
    }

    //商品列表
    public function goodslist()
    {

        $m    = $this->model;
        $user = $m->checkAuth();
        if ($user) {
            if ($_GET['caid']) {
                $para['caid'] = intval($_GET['caid']);
            }

            if ($_GET['brand_id']) {
                $para['brand_id'] = intval($_GET['brand_id']);
            }
            if (intval($_GET['page']) > 0) {
                $page = intval($_GET['page']);
            } else {
                $page = 1;
            }
            if (trim($_GET['keywords'])) {

                $para['keywords'] = $_GET['keywords'];
            }
            if (trim($_GET['goods_ids'])) {

                $para['goods_ids'] = $_GET['goods_ids'];
            }
            if (intval($_GET['order'])) {

                $para['order'] = $_GET['order'];
            }

            if (intval($_GET['price'])) {

                $para['price'] = $_GET['price'];
            }

            $para['sort_type'] = '';
            $para['sort_val']  = 1;

            if (trim($_GET['sort_type'])) {
                $para['sort_type'] = intval($_GET['sort_type']);
            }
            if ($_GET['sort_val']) {
                $para['sort_val'] = intval($_GET['sort_val']);
            }

            if (intval($_GET['uid'])) {
                $para['uid'] = intval($_GET['uid']);
            } else {
                $para['uid'] = 0;
            }

            $data = $m->getGoodsList($para, $page);

            //获取页码

            if ($data['maxpage'] > 1) {
                $data['spage'] = $m->fun->spage($data['page'], $data['maxpage']);
            }

            $this->view->JSAPI = $m->getJSApiData();
            //print_r($data);die;
            if (trim($_GET['act']) == "ajax") {

                echo json_encode($data);die();

            } else {

                if ($_GET['caid']) {
                    $name = $data['caid'];
                }if ($_GET['brand_id']) {
                    $name = $data['brand']['name'];
                }if (trim($_GET['keywords'])) {
                    $name = $_GET['keywords'];
                }
                $this->view->name = $name;
                $this->view->data = $data;
                $this->view->user = $user;
                $this->display();

            }
        }

    }

    //点击详情页返回之前数据
    public function ajaxgoodslist()
    {
        $m    = $this->model;
        $user = $m->checkAuth(false);
        //if ($user) {

        if ($_GET['act'] = "ajax" && !empty($_GET['cook_type'])) {

            $arr               = explode('-', $_GET['cook_type']);
            $_GET['caid']      = $arr[0];
            $_GET['brand_id']  = $arr[1];
            $_GET['goods_ids'] = $arr[2];
            $_GET['keywords']  = $arr[3];
            $_GET['order']     = $arr[4];
            $_GET['price']     = $arr[5];

        }
        if (intval($_GET['page']) > 0) {
            $page = intval($_GET['page']);
        } else {
            $page = 1;
        }
        if (trim($_GET['keywords'])) {

            $para['keywords'] = $_GET['keywords'];
        } else {

            if (trim($_GET['goods_ids'])) {

                $para['goods_ids'] = $_GET['goods_ids'];
            }

            if ($_GET['caid']) {
                $para['caid'] = intval($_GET['caid']);
            }

            if ($_GET['brand_id']) {
                $para['brand_id'] = intval($_GET['brand_id']);
            }

        }

        if (intval($_GET['order'])) {

            $para['order'] = $_GET['order'];
        }

        if (intval($_GET['price'])) {

            $para['price'] = $_GET['price'];
        }
        $para['type'] = "return";
        $data         = $m->getGoodsList($para, $page);

        echo json_encode($data);die();

        //}

    }

    //商品详情
    public function goodsdetail()
    {

        $m    = $this->model;
        $user = $m->checkAuth();
        if ($user) {

            $next = true;

            if ($_GET['recode'] != "") {
                $recode = intval($_GET['recode']);
                if ($recode != "") {
                    $_SESSION['qrcode_recode'] = $recode;
                }
            } else {
                $pro = $m->getRecode();
                if ($pro['recode'] != "") {
                    $this->redirect("/index/goodsdetail?goods_id=" . $_GET['goods_id'] . "&recode=" . $pro['recode']);
                    $next = false;
                }
            }

            if ($next) {

                $goods_id = intval($_GET['goods_id']);
                if ($goods_id > 0) {

                    $data = $m->getSingleGoods($goods_id);

                }

                if ($data) {
                    // print_r($data);
                    if ($data['is_on_sale'] == 1) {

                        $this->view->member_level = $m->getMemberLevel();

                        $this->view->user = $user;

                        if ($data['relevance_id'] > 0) {
                            $this->view->reles = $m->getRelevances($data['relevance_id']);
                        }

                        if ($data['small_docid']) {
                            $picid    = explode(",", $data['small_docid']);
                            $pic_data = $m->getDocs($picid);
                            if ($pic_data) {
                                foreach ($pic_data as $k => $v) {
                                    $data['docs'][$k] = $v;
                                }
                            }
                        }

                        //会员收藏
                        /*$state = $m->getState($shop['shopid'], $user['uid'], $goods_id);
                        if ($state) {
                        $this->view->state = $state['state'];
                        }*/

                        $this->view->data    = $data;
                        $this->view->app     = $user['app'];
                        $this->view->cartnum = $m->getCartNum();
                       $this->view->JSAPI   = $m->getJSApiData();
                        //添加商品浏览记录
                        $m->addGoodsClickCount($goods_id);

                        $this->display("goodsdetail");

                    } else {
                        $this->display('goodsdetail_off');
                    }

                } else {
                    $this->redirect("/index/cate");
                }
            }

        }

    }

    //登录
    public function signin()
    {

        $this->display();
    }

    // AJAX
    public function ajax()
    {
        $data['code'] = 900;

        $m = $this->model;

        // 登录
        if ($_POST['act'] == 'signin') {

            $data = $m->doSignin($_POST);

        }
        // 收藏商品
        elseif ($_POST['act'] == "collection") {
            $data = $m->collectionGoods($_POST['goods_id']);
        }
        //
        elseif ($_POST['act'] == "spec") {
            $data = $m->getStockInfo($_POST['caid_str'],$_POST['goods_id']);
        }

        echo json_encode($data);
    }

    //定时显示最新订单信息
    public function getneworderdetail()
    {

        $m = $this->model;

        $data = $m->getNewOrderDetail();
        echo json_encode($data);

    }
}
